package com.jwdf.uda.view;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ItemDropMenuBinding;
import com.jwdf.uda.databinding.ViewMenuListBinding;
import com.jwdf.uda.ui.adapter.AbsAdapter;
import com.jwdf.uda.ui.adapter.AbsViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2016/5/27.
 */
public class FilterMenu extends FrameLayout {
    private LinearLayout mContainer;
    private TextView mFilterText;
    private PopupWindow mDropDownMenu;
    private MenuItemAdapter mAdapter;
    private int menuWidth;
    //    private MenuItemAdapter.OnMenuItemSelectedListener mListener;

    public FilterMenu(Context context, int resId) {
        this(context);
        initDropDownMenu(resId);
    }

    public FilterMenu(Context context) {
        super(context);
        LayoutInflater.from(getContext()).inflate(R.layout.view_filter_menu, this, true);
        initDropDownMenu();
        mContainer = (LinearLayout) findViewById(R.id.ll_filter_menu);
        mFilterText = (TextView) findViewById(R.id.tv_filter_item);
        mContainer.setOnClickListener(createOnClickListener());
        mFilterText.setOnClickListener(createOnClickListener());
    }
    private void initDropDownMenu() {
        View menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_menu_list, null);
        ViewMenuListBinding menuListBinding = DataBindingUtil.bind(menuView);
        mAdapter = new MenuItemAdapter(getContext());
        menuListBinding.menuList.setAdapter(mAdapter);
        mDropDownMenu = new PopupWindow(menuView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mDropDownMenu.setFocusable(true);
        mDropDownMenu.setOutsideTouchable(true);
        mDropDownMenu.setBackgroundDrawable(new BitmapDrawable());
        menuView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        menuWidth = menuView.getMeasuredWidth();
    }
    private OnClickListener createOnClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDropDownMenu();
            }
        };
    }
    private void showDropDownMenu() {
        if(mAdapter.getCount() == 0) {
            return;
        }
        int xOff = (mContainer.getWidth() - menuWidth) / 2;
        mDropDownMenu.showAsDropDown(mFilterText, xOff, 0);
    }
    private void initDropDownMenu(int resId) {
        View menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_menu_list, null);
        ViewMenuListBinding menuListBinding = DataBindingUtil.bind(menuView);
        mAdapter = new MenuItemAdapter(getContext());
        menuListBinding.menuList.setBackgroundResource(resId);
        menuListBinding.menuList.setAdapter(mAdapter);
        mDropDownMenu = new PopupWindow(menuView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mDropDownMenu.setFocusable(true);
        mDropDownMenu.setOutsideTouchable(true);
        mDropDownMenu.setBackgroundDrawable(new BitmapDrawable());
        menuView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        menuWidth = menuView.getMeasuredWidth();
    }
    public void bindMenuData(List<String> items) {
        mAdapter.setData(items);
    }
    public void setSelectedItemText(String item) {
        int length = item.length();
        if(length > 6)
            item = item.substring(0, 6) + "...";

        mFilterText.setText(item);
    }
    public void setOnMenuItemSelectedListener(MenuItemAdapter.OnMenuItemSelectedListener listener) {
        if(listener != null) {
            mAdapter.setOnMenuItemSelectedListener(listener);
        }
    }
    public void dismissFilterMenu() {
        if(mDropDownMenu.isShowing()) {
            mDropDownMenu.dismiss();
        }
    }
    public void show(View anchor) {
        if(mAdapter.getCount() == 0) {
            return;
        }
        int xOff = (anchor.getWidth()-menuWidth)/2;
        mDropDownMenu.showAsDropDown(anchor, xOff, 0);
    }

    public static class MenuItemAdapter extends AbsAdapter<String> {
        private OnMenuItemSelectedListener mListener;

        public MenuItemAdapter(Context context) {
            super(context);
        }

        public void setOnMenuItemSelectedListener(OnMenuItemSelectedListener listener) {
            this.mListener = listener;
        }

        @Override
        protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
            return new MenuItemViewHolder(mInflater.inflate(R.layout.item_drop_menu, parent, false));
        }

        public interface OnMenuItemSelectedListener {
            void onMenuItemSelected(String item, int position);
        }
        public class MenuItemViewHolder extends AbsViewHolder<String> {
            private ItemDropMenuBinding mBinding;

            public MenuItemViewHolder(View convertView) {
                super(convertView);
                mBinding = DataBindingUtil.bind(convertView);
            }
            @Override
            protected void onBindView(String item, final int position) {
                mBinding.menuItemView.setTag(R.id.tag, item);
                int length = item.length();
                if(length > 6)
                    item = item.substring(0, 6) + "...";

                mBinding.tvMenuItem.setText(item);
                mBinding.menuItemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mListener != null) {
                            mListener.onMenuItemSelected((String) v.getTag(R.id.tag), position);
                        }
                    }
                });
            }
            @Override
            protected void onBindView(String item) {

            }
        }
    }
}
